<?php
/*
	Get page score based on link loss
*/

class page_score extends seo_engine
{
	public function map_rss( $feed_url )
	{
		$feed_url = core::_doRequest($feed_url);

		if( preg_match_all("#<link>(.*?)</link>#", $feed_url, $feed_link))
			var_dump($feed_link);
		else
		{
			echo "Not found!";
		}
		return $feed_link;
	}

	public function map_sitemap( $sitemap_url )
	{
		$sitemap_url = core::_doRequest($sitemap_url);

		if( preg_match_all("#<loc>(.*?)</loc>.*?<priority>(.*?)</priority>#s", $sitemap_url, $sitemap_link))
			return $sitemap_link;

	}

	public function scan_page_links( $url )
	{
		$origin_url = $url;

		if( $this->trimt_to_root($url) )
		 	$domain_root = $this->trimt_to_root($url);
		else
		{
			echo "EXCEPTION: Not a valid link $url".PHP_EOL;
			return false;
		}

		$url = core::_doRequest($url);

		if( !preg_match_all('#<a(.*?)href="(.*?)"#s', $url, $url_all))
		{
			// not found any links
			return false;
		}

		$url_all[1] = $url_all[2];

		// store links int + ext
		$tmp['root'] = $domain_root;

		for($i = 0; $i< count($url_all[1]); $i++)
		{
			// case when internal link (fix)
			if( substr($url_all[1][$i],0,4) != "http" )
				$url_all[1][$i] = $domain_root.$url_all[1][$i];
			
			$url_all[1][$i] = $this->filter_url($url_all[1][$i]);

			//echo "INPUT:".$url_all[1][$i].PHP_EOL;

			if( preg_match('#'.$domain_root.'(\S*)#s', $url_all[1][$i], $url_internal))
				$tmp['internal'][$url_internal[1]] = '';
			
			else
				$tmp['external'][$url_all[1][$i]] = '';
			

			
		}
		
		
		if(isset($tmp['internal']))
			$internal = count($tmp['internal']);
		else
			$internal = 0;

		if(isset($tmp['external']))
			$external = count($tmp['external']);
		else
			$external = 0;

		$tmp['score'] = $this->page_score( $origin_url, $internal, $external, true);
 
		return $tmp;
	}

	/*
		Input(@array): scan multiple links
		Output(@array): scores
	*/
	function scan_multiple($arr)
	{
		if(!is_array($arr))
			return false;

		foreach($arr as $url)
		{
			$tmp[] = $this->scan_page_links($url);
		}

		return $tmp;
	}


	/*
	 * Parse web 
	 */
	function web_parse($url)
	{
		$str = core::_doRequest($url);
		if( preg_match_all('#<a.*?href=("|\')(http://|https://)(.*?)("|\')#', $str, $findUrl))
		{
			//var_dump($findUrl);
			for( $i=0; $i<count($findUrl[3]); $i++)
			{
				// all links
				$k['url'][$i] 		= $findUrl[2][$i].$findUrl[3][$i];
				// filtered link
				$k['link'][$i] 		= str_replace( array('http://', 'https://'), '', $k['url'][$i] );
				// remove last /
				if( $k['link'][$i][ strlen($k['link'][$i]) - 1] == '/')
					$k['link'][$i] = substr($k['link'][$i], 0, (strlen($k['link'][$i]) - 1));
				
				// root
				$k['root'][$i]	 = trimt_to_root($k['url'][$i]);
				// remove www.
				if( substr($k['root'][$i], 0 , 4) == 'www.')
					$k['root'][$i] = substr( $k['root'][$i], 4);
				
				// dom ext
				$k['ext'][$i] 		= substr($k['root'][$i], strrpos($k['root'][$i], '.') + 1);
				
			}
		}
		
		// remove duplicates
		$k['root'] = array_unique($k['root']);
		
		return $k;
	}

	public function page_score($url, $linkloss_int, $linkloss_ext, $raw = false)
	{
		$total = $linkloss_int + $linkloss_ext;
		$intRaport = 96;

		switch($total)
    	{
    		case ($total <= 10): //great
		    $score = 100/($intRaport +  (70/100) * $linkloss_int + $linkloss_ext);
		    break;

    		case ($total <= 20):
		    $score = 100/($intRaport +  (50/100) * $linkloss_int + $linkloss_ext);
		    break;

		    case ($total <= 50):
		    $score = 100/($intRaport +  (40/100) * $linkloss_int + $linkloss_ext);
		    break;

		    case ($total <= 100):
		    $score = 100/($intRaport +  (35/100) * $linkloss_int + $linkloss_ext);
		    break;

		    case ($total <= 150):
		    $score = 100/($intRaport +  (30/100) * $linkloss_int + $linkloss_ext);
		    break;

		    case ($total <= 250):
		    $score = 100/($intRaport + (25/100) * $linkloss_int + $linkloss_ext);
		    break;

		    case ($total > 250): //possible spam
		    $score = 100/($intRaport + (20/100) * $linkloss_int + $linkloss_ext);
		    break;
    	}

    	if( $raw )
    		$score = round($score*100,2);

    	return $score;
	    
	}

	public function get_score($url)
	{
		if(!is_array($url))
			return false;

		for($i = 0; $i < count($url); $i++)
		{
			$tmp =  $this->scan_page_links( $url[$i] );
			$x[$url[$i]] = $tmp['score'];
		}

		return $x[$i];
	}

}
?>